<template>
  <div class="iconfont-container">
    <div class="icon-body">
      <el-input v-model="name" style="position: relative;" clearable placeholder="请输入图标名称" @clear="reset"
                @input.native="filterIcons"
      >
        <i slot="suffix" class="el-icon-search el-input__icon" />
      </el-input>
      <div class="icon-list">
        <div v-for="(v, index) in tempIconList" :key="index" @click="selectedIcon(v)">
          <i :class="v" class="iconfont" />
          <span>{{ v }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import initIconfont from '@/utils/getStyleSheets'

export default {
  name: 'IocnfontSelect',
  data() {
    return {
      name: '',
      iconList: [],
      tempIconList: []
    }
  },
  created() {
    initIconfont.ali().then((res) => (this.iconList = res))
  },
  methods: {
    filterIcons() {
      this.tempIconList = this.iconList.filter((item) => item.includes(this.name))
    },
    selectedIcon(name) {
      this.$emit('selected', `${name} iconfont`)
      document.body.click()
    },
    reset() {
      this.name = ''
      this.tempIconList= this.iconList
    }
  }
}
</script>

<style scoped lang="scss">
.iconfont-container {
  .icon-body {
    width: 100%;
    padding: 10px;

    .icon-list {
      height: 200px;
      overflow-y: scroll;

      div {
        height: 62px;
        line-height: 30px;
        margin-bottom: -5px;
        cursor: pointer;
        width: 33%;
        float: left;
        text-align: center;
      }
      span {
        display: block;
        vertical-align: -0.15em;
        fill: currentColor;
        overflow: hidden;
        overflow:hidden; //超出的文本隐藏
        text-overflow:ellipsis; //用省略号显示
        white-space:nowrap; //不换行
      }
    }
  }
}
</style>
